//
//  RJBar.m
//  AirQuality
//
//  Created by Po on 15/4/2.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJBar.h"
@interface RJBar ()

@property (assign, nonatomic) CGFloat maxNum;       //最大值界限
@property (assign, nonatomic) CGFloat dataNum;      //实际数值
@property (assign, nonatomic) UIColor * color;      //图颜色

@end

@implementation RJBar
- (instancetype)initWithFrame:(CGRect)frame maxNum:(CGFloat)maxNum dataNum:(CGFloat)dataNum color:(UIColor *)color
{
    self = [super initWithFrame:frame];
    if (self) {
        _maxNum = maxNum;
        _dataNum = dataNum;
        _color = color;
        [self setBackgroundColor:_color];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
//    [self drawShadow:rect];
//    [self drawBar:rect];
}

/**绘制阴影*/
- (void)drawShadow:(CGRect)rect
{
    CGContextRef contextref = UIGraphicsGetCurrentContext();
    for (NSUInteger i = 0; i < 2; i ++) {
        CGContextSetLineWidth(contextref, 2.0);
        CGContextSetStrokeColorWithColor(contextref, [UIColor blueColor].CGColor);
        CGRect rectangle = CGRectMake(0,rect.size.height - 15,rect.size.width,14);
        CGContextAddEllipseInRect(contextref, rectangle);
        if (i == 1) {
            CGContextSetAlpha(contextref, 0.5);
        }
        CGContextSetFillColorWithColor(contextref, (i == 0) ? [UIColor blackColor].CGColor : _color.CGColor);
        CGContextFillPath(contextref);
        CGContextStrokePath(contextref);
    }
    CGContextSetAlpha(contextref, 1);
}

/**绘制柱状图*/
- (void)drawBar:(CGRect)rect
{
    if (rect.size.height == 0) {
        return;
    }
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGFloat sixteenHalfWidth = rect.size.width / 32;
    CGFloat margin = 5;
    CGFloat marginTop = width / 2;
    CGFloat barY = rect.size.height - margin - height;
    
    CGContextRef contextref = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(contextref, 1.0f);
    CGContextSetRGBStrokeColor(contextref, 0.0, 1.0, 1.0, 1.0);
    //设定起始位置
    CGContextMoveToPoint(contextref, margin, rect.size.height - marginTop);
    //左侧边
    CGContextAddLineToPoint(contextref, margin, barY);
    //上弧边
    CGContextAddCurveToPoint(contextref, margin + sixteenHalfWidth, barY - marginTop, width - sixteenHalfWidth - margin , barY - marginTop, width - margin, barY);
    //右侧边
    CGContextAddLineToPoint(contextref, width - margin, rect.size.height - marginTop);
    //下弧边
    CGContextAddCurveToPoint(contextref, width - sixteenHalfWidth - margin, rect.size.height, margin + sixteenHalfWidth, rect.size.height, margin, rect.size.height - marginTop);
    CGContextAddRect(contextref, CGRectMake(margin, barY, width, height));
    CGContextSetFillColorWithColor(contextref, _color.CGColor);
    CGContextFillPath(contextref);
    //添加路径
    CGContextStrokePath(contextref);
}
@end
